{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#引入相关函数库\n",
    "import pandas as pd\n",
    "import numpy as np\n",
    "import glob\n",
    "import os\n",
    "from osgeo import gdal,gdal_array\n",
    "#import glob\n",
    "from tqdm import tqdm\n",
    "import multiprocessing as mp\n",
    "import argparse\n",
    "import time\n",
    "import random\n",
    "from skimage.feature import greycomatrix, greycoprops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "def getminmaxatpercent(im,min_perc=0.02,max_perc=0.98):\n",
    "    im_fla = im.flatten().astype('int')\n",
    "    minv = im.min()\n",
    "    maxv = im.max()\n",
    "    hist = np.zeros(maxv.astype('int')+1)\n",
    "#     hist = hist.astype('int')\n",
    "\n",
    "#    histgram\n",
    "    for i in np.arange(np.size(im_fla)):\n",
    "        hist[im_fla[i]] = hist[im_fla[i]]+1\n",
    "    \n",
    "    hist = hist/np.sum(hist)\n",
    "    \n",
    "    hist_cum = np.cumsum(hist)\n",
    "    \n",
    "    for i in np.arange(len(hist_cum)):\n",
    "        if hist_cum[i] >= min_perc:\n",
    "            minv = i\n",
    "            break\n",
    "    for i in np.arange(len(hist_cum)-1,0,-1):    \n",
    "        if hist_cum[i] <= max_perc:\n",
    "            maxv = i\n",
    "            break\n",
    "    return minv,maxv\n",
    "#拉伸函数\n",
    "def stretch(im,minv,maxv,ngrey = 256,gamma = None):\n",
    "    im = np.where(im >minv,im,minv)\n",
    "    im = np.where(im <maxv,im,maxv)\n",
    "    tmp = (im - minv)*1.0/(maxv - minv)\n",
    "    if gamma == None:\n",
    "        im = np.uint8(tmp*(ngrey-1)+0.5)\n",
    "    else:\n",
    "        im = np.uint8((ngrey-1)*np.power(tmp,1.0/gamma))\n",
    "    return im\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "##########\n",
    "# 根据指定的窗口参数，计算灰度共生矩阵6各纹理参数\n",
    "# windbuf 图像数据\n",
    "# step 灰度共生矩阵参数 \n",
    "#\n",
    "#######\n",
    "\n",
    "def glcm_feas(winbuf,step = 1,angle = np.pi/4):\n",
    "    try:\n",
    "        comatrix = greycomatrix(winbuf,[step],[angle],256,symmetric=True,normed= True)\n",
    "        corr = greycoprops(comatrix,prop = 'correlation')\n",
    "        con = greycoprops(comatrix,prop = 'contrast')\n",
    "        dissimi = greycoprops(comatrix,prop = 'dissimilarity')\n",
    "        homo = greycoprops(comatrix,prop = 'homogeneity')\n",
    "        asm = greycoprops(comatrix,prop = 'ASM')\n",
    "        energy = greycoprops(comatrix,prop = 'energy')\n",
    "        return (corr,con,dissimi,homo,asm,energy)\n",
    "    except:\n",
    "        return (0,0,0,0,0,0)\n",
    "    \n",
    "'''\n",
    "类似greycoprops函数，计算灰度共生矩阵的熵、均值、标准差\n",
    "prop :{entropy,mean,variance}\n",
    "'''   \n",
    "\n",
    "def glcm_other_feas(P,prop='entropy'):\n",
    "    (num_level,num_level2,num_dist,num_angle) = P.shape\n",
    "    if num_level !=num_level2:\n",
    "        raise ValueError('num_level and num_level2 must be equal!')\n",
    "    if num_dist <= 0:\n",
    "        raise ValueError('num_dist must be positive')\n",
    "    if num_angle<=0:\n",
    "        raise ValueError('num_angle must be positive')\n",
    "        \n",
    "    # normalize each GLCM\n",
    "    P = P.astype(np.float64)\n",
    "    glcm_sums = np.apply_over_axes(np.sum, P, axes=(0, 1))\n",
    "    glcm_sums[glcm_sums == 0] = 1\n",
    "    P /= glcm_sums\n",
    "    \n",
    "    # create weights for specified property\n",
    "    \n",
    "    if prop == 'entropy':\n",
    "        mask0 = P < 1e-15\n",
    "        P[mask0] = 1\n",
    "        results = np.apply_over_axes(np.sum,(-P*np.log(P)),axes=(0,1))[0,0]\n",
    "    elif prop=='mean':\n",
    "        I = np.array(range(num_level)).reshape((num_level, 1, 1, 1))\n",
    "        J = np.array(range(num_level)).reshape((1, num_level, 1, 1))\n",
    "        mean_i = np.apply_over_axes(np.sum, (I * P), axes=(0, 1))[0, 0]\n",
    "        mean_j = np.apply_over_axes(np.sum, (J * P), axes=(0, 1))[0, 0]\n",
    "        \n",
    "#        results=(mean_i+mean_j) /2\n",
    "        results = mean_i\n",
    "    elif prop=='variance':\n",
    "        I = np.array(range(num_level)).reshape((num_level, 1, 1, 1))\n",
    "        J = np.array(range(num_level)).reshape((1, num_level, 1, 1))\n",
    "        diff_i = I - np.apply_over_axes(np.sum, (I * P), axes=(0, 1))[0, 0]\n",
    "        diff_j = J - np.apply_over_axes(np.sum, (J * P), axes=(0, 1))[0, 0]\n",
    "        std_i = np.sqrt(np.apply_over_axes(np.sum, (P * (diff_i) ** 2),\n",
    "                                           axes=(0, 1))[0, 0])\n",
    "        std_j = np.sqrt(np.apply_over_axes(np.sum, (P * (diff_j) ** 2),\n",
    "                                           axes=(0, 1))[0, 0])\n",
    "#        results= (std_i +std_j) /2\n",
    "        results = std_i\n",
    "    \n",
    "    return results"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "\"\"\"\n",
    "imgbuf: 是原始数据往外扩大winsize/2的大小，返回的是减去了winsize/2后\n",
    "winsize\n",
    "step, angle\n",
    "\n",
    "\"\"\" \n",
    "def paddedbuf(im,winsize):\n",
    "    off = int(winsize/2)\n",
    "    ny,nx = im.shape\n",
    "    padded = np.zeros((ny + 2*off,nx+2*off),dtype = np.uint8)\n",
    "    padded[off:off+ny,off:off+nx] = im\n",
    "    \n",
    "    up = padded[off+1:2*off+1,off:-off][::-1]\n",
    "    padded[:off,off:-off] = up\n",
    "    \n",
    "    bottom = padded[ny-1:off+ny-1,off:-off][::-1]\n",
    "    padded[off+ny:,off:-off] = bottom\n",
    "    \n",
    "    left = padded[:,off+1:2*off+1][:,::-1]\n",
    "    padded[:,0:off]= left\n",
    "    \n",
    "    right = padded[:,nx-1:nx-1+off][:,::-1]\n",
    "    padded[:,nx+off:] = right\n",
    "    return padded\n",
    "\n",
    "def glcm_by_win_scan(imgbuf,winsize=3,step = 1, angle = np.pi/4,level = 256):\n",
    "    nx = imgbuf.shape[1]\n",
    "    ny = imgbuf.shape[0]\n",
    "    hwin = int(winsize/2)\n",
    "    \n",
    "    outx = nx - winsize +1\n",
    "    outy = ny - winsize +1\n",
    "    \n",
    "    contrastraster = np.zeros((outy,outx), dtype = float)\n",
    "    contrastraster[:] = 0.0\n",
    "    \n",
    "    dissimilarityraster = np.zeros((outy,outx), dtype = float)\n",
    "    dissimilarityraster[:] = 0.0\n",
    "    \n",
    "\n",
    "    homogeneityraster = np.zeros((outy,outx), dtype = float)\n",
    "    homogeneityraster[:] = 0.0\n",
    "    \n",
    "\n",
    "#    energyraster = np.zeros((outy,outx), dtype = float)\n",
    "#    energyraster[:] = 0.0\n",
    "        \n",
    "    correlationraster = np.zeros((outy,outx), dtype = float)\n",
    "    correlationraster[:] = 0.0\n",
    "    \n",
    "    ASMraster = np.zeros((outy,outx), dtype = float)\n",
    "    ASMraster[:] = 0.0\n",
    "    \n",
    "    meanraster = np.zeros((outy,outx), dtype = float)\n",
    "    meanraster[:]= 0\n",
    "    \n",
    "    varraster = np.zeros((outy,outx), dtype = float)\n",
    "    varraster[:]=0\n",
    "    \n",
    "    entroraster = np.zeros((outy,outx), dtype = float)\n",
    "    entroraster[:]=0\n",
    "    \n",
    "#    for jo in tqdm(range(outy)):\n",
    "    for jo in range(outy):\n",
    "#    for io in range(outx):\n",
    "        for io in range(outx):\n",
    "            i = io + hwin\n",
    "            j = jo + hwin\n",
    "            if j >=ny:\n",
    "                continue\n",
    "            glcm_win = imgbuf[j-hwin:j+hwin+1,i-hwin:i+hwin+1]\n",
    "#            try:\n",
    "            glcm = greycomatrix(glcm_win,[step],[angle],levels=level,symmetric=True,normed= True)\n",
    "#                glcm = greycomatrix(glcm_win,[step],[angle],levels=level,normed= True)\n",
    "            correlationraster[jo,io] = greycoprops(glcm,prop = 'correlation')\n",
    "            contrastraster[jo,io] = greycoprops(glcm,prop = 'contrast')\n",
    "            dissimilarityraster[jo,io] = greycoprops(glcm,prop = 'dissimilarity')\n",
    "            homogeneityraster[jo,io] = greycoprops(glcm,prop = 'homogeneity')\n",
    "            ASMraster[jo,io] = greycoprops(glcm,prop = 'ASM')\n",
    "#                energyraster[jo,io] = greycoprops(glcm,prop = 'energy')\n",
    "            entroraster[jo,io] = glcm_other_feas(glcm,prop='entropy')\n",
    "            meanraster[jo,io] = glcm_other_feas(glcm,prop='mean')\n",
    "            varraster[jo,io] = glcm_other_feas(glcm,prop='variance')\n",
    "            glcm = None\n",
    "                #glcm_win = None\n",
    "#            except:\n",
    "#                correlationraster[jo,io] = 0\n",
    "#                contrastraster[jo,io] = 0\n",
    "#                dissimilarityraster[jo,io] = 0\n",
    "#                homogeneityraster[jo,io] = 0\n",
    "#                ASMraster[jo,io] = 0\n",
    "##                energyraster[jo,io] = 0\n",
    "#                entroraster[jo,io] = 0\n",
    "#                meanraster[jo,io] = 0\n",
    "#                varraster[jo,io] = 0\n",
    "    \n",
    "    return meanraster,varraster,entroraster,contrastraster, dissimilarityraster, homogeneityraster, correlationraster, ASMraster\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#'''计算植被指数NDVI'''\n",
    "def cal_ndvi(red,nir):\n",
    "#'''NIR:B8;RED:B4'''       \n",
    "    ndvi = (nir-red)/(nir+red)\n",
    "    ndvi[np.isnan(ndvi)] = 0\n",
    "    return ndvi\n",
    "#'''计算水体指数（以哨兵2A数据波段为例）'''\n",
    "def cal_ndwi(green,nir):\n",
    "    #'''GREEN:B3'''\n",
    "    ndwi = (green-nir)/(green+nir)\n",
    "    ndwi[np.isnan(ndwi)] = 0\n",
    "    return ndwi\n",
    "def cal_Mndwi(green,SWIR1):\n",
    "    Mndwi = (green-SWIR1)/(green+SWIR1)\n",
    "    Mndwi[np.isnan(Mndwi)] = 0\n",
    "    return Mndwi \n",
    "def cal_AWEI_nsh(green,nir,SWIR1,SWIR2):\n",
    "#'''SWIR1:B11;SWIR2:B12'''\n",
    "    AWEI_nsh = (4*(green-SWIR1)-(0.25*nir + 2.75*SWIR2))/(green + nir + SWIR1 + SWIR2)\n",
    "    AWEI_nsh[np.isnan(AWEI_nsh)] = 0\n",
    "    return AWEI_nsh\n",
    "def cal_AWEI_sh(blue,green,nir,SWIR1,SWIR2):\n",
    "#'''BLUE:B2'''\n",
    "    AWEI_sh = blue + 2.5*green - 1.5*(nir + SWIR1) - 0.25*SWIR2\n",
    "    AWEI_sh[np.isnan(AWEI_sh)] = 0\n",
    "    return AWEI_sh\n",
    "def cal_WI2015(green,red,nir,SWIR1,SWIR2):\n",
    "    WI2015 = 1.7204 + 171*green + 3*red - 70*nir - 45*SWIR1 - 71*SWIR2\n",
    "    WI2015[np.isnan(WI2015)] = 0\n",
    "    return WI2015"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "def saveim(filename,im,srs=None,geo= None):\n",
    "    if len(im.shape)>2:\n",
    "        nb,nrow,ncol = im.shape\n",
    "        driver = gdal.GetDriverByName('GTiff')    \n",
    "        outDs = driver.Create(filename,ncol,nrow,nb,gdal.GDT_Float32)\n",
    "        if None is not geo:\n",
    "            outDs.SetGeoTransform(geo)\n",
    "        if None is not srs:\n",
    "            outDs.SetProjection(srs)\n",
    "        for i in range(nb):\n",
    "            outBand = outDs.GetRasterBand(i+1)\n",
    "            outBand.WriteArray(im[i,:,:])\n",
    "    else:\n",
    "        nrow,ncol = im.shape\n",
    "        nb = 1\n",
    "        driver = gdal.GetDriverByName('GTiff')    \n",
    "        outDs = driver.Create(filename,ncol,nrow,nb,gdal.GDT_Float32)\n",
    "        if None is not geo:\n",
    "            outDs.SetGeoTransform(geo)\n",
    "        if None is not srs:\n",
    "            outDs.SetProjection(srs)       \n",
    "            outBand = outDs.GetRasterBand(nb)\n",
    "            outBand.WriteArray(im)\n",
    "\n",
    "    \n",
    "    outBand = None\n",
    "    outDs = None\n",
    "# def saveim_multiproc(filename,im,srs,geo):\n",
    "#     nrow,nb,ntpro,ncol = im.shape\n",
    "#     driver = gdal.GetDriverByName('GTiff')    \n",
    "#     outDs = driver.Create(filename,ncol,nrow,nb,gdal.GDT_Float32)\n",
    "#     if None is not geo:\n",
    "#         outDs.SetGeoTransform(geo)\n",
    "#     if None is not srs:\n",
    "#         outDs.SetProjection(srs)\n",
    "#     tmp = np.swapaxes(im,0,2).reshape((nb,nrow,ncol))\n",
    "#     for i in range(nb):\n",
    "#         outBand = outDs.GetRasterBand(i+1)\n",
    "#         outBand.WriteArray(tmp[i,:,:])\n",
    "    \n",
    "#     outBand = None\n",
    "#     outDs = None\n",
    "\n",
    "# def paddedbuf(im,winsize):\n",
    "#     off = int(winsize/2)\n",
    "#     ny,nx = im.shape\n",
    "#     padded = np.zeros((ny + 2*off,nx+2*off),dtype = np.uint8)\n",
    "#     padded[off:off+ny,off:off+nx] = im\n",
    "    \n",
    "#     up = padded[off+1:2*off+1,off:-off][::-1]\n",
    "#     padded[:off,off:-off] = up\n",
    "    \n",
    "#     bottom = padded[ny-1:off+ny-1,off:-off][::-1]\n",
    "#     padded[off+ny:,off:-off] = bottom\n",
    "    \n",
    "#     left = padded[:,off+1:2*off+1][:,::-1]\n",
    "#     padded[:,0:off]= left\n",
    "    \n",
    "#     right = padded[:,nx-1:nx-1+off][:,::-1]\n",
    "#     padded[:,nx+off:] = right\n",
    "#     return padded\n",
    "    \n",
    "# def simulate_data():\n",
    "#     winsize = 7\n",
    "#     ngrey = 256\n",
    "#     nr = 100\n",
    "#     nc = 100\n",
    "#     nb = 1\n",
    "#     random.seed(100)\n",
    "#     data = np.random.randint(0,ngrey-1,(nb,nr,nc),dtype=np.uint8)\n",
    "# #    saveim('input.tif',np.expand_dims(data,axis = 0),srs=None,geo=None)\n",
    "# #    saveim('input.tif',data,srs=None,geo=None)\n",
    "    \n",
    "# #    basename = os.path.basename(imgfile).split('.')\n",
    "# #    basedir = os.path.dirname(imgfile)\n",
    "# #    basedir = './glcm'\n",
    "#     for i in range(nb):\n",
    "#         t1 = time.time()\n",
    "# #        minv,maxv = getminmaxatpercent(im[i,:,:])\n",
    "# #        stretchim = stretch(im[i,:,:],minv,maxv,ngrey,1.8)\n",
    "#         padded = paddedbuf(data[i,:,:],winsize)\n",
    "#         mea,var,entr,con,diss,homo,corr,ASM = glcm_by_win_scan(padded,winsize,1,np.pi/4, ngrey)\n",
    "        \n",
    "        \n",
    "# #        basename = ['input','tif']\n",
    "# #        basedir = './'\n",
    "# #        outname = os.path.join(basedir,basename[0]+'_b{}_win{}_texture.tif'.format(i,winsize))\n",
    "    \n",
    "# #        saveim(outname,np.array([mea,var,entr,con,diss,homo,corr,ASM]),srs=srs,geo=geo)\n",
    "# #        saveim(outname,np.array([mea,var,entr,con,diss,homo,corr,ASM]),srs=None,geo=None)\n",
    "#         t2 = time.time()\n",
    "        \n",
    "#         print('single process is ok! total time is {},processing file {},band {} \\n'.format((t2 -t1)/60.0,'ty',i))\n",
    "def single_proc_file(imgfile,winsize,ngrey = 256):\n",
    "#    imgfile = './data/rgbnir/row1024_col2048.tif'\n",
    "#    imgfile = 'D:\\\\data\\\\hn\\\\高分2号新乡分类shape\\\\rgbnir_subset\\\\row1024_col2048.tif'\n",
    "#    winsize = 3\n",
    "#    off = int(winsize/2)\n",
    "    \n",
    "    \n",
    "#    data = np.random.randint(0,63,(1,15,20),dtype=np.uint8)\n",
    "#    saveim('input.tif',np.expand_dims(data,axis = 0),srs=None,geo=None)\n",
    "#    saveim('input.tif',data,srs=None,geo=None)\n",
    "    \n",
    "#    imgfile = './input.tif'\n",
    "    ds = gdal.Open(imgfile)\n",
    "    nx = ds.RasterXSize\n",
    "    ny = ds.RasterYSize\n",
    "##    \n",
    "    nb = ds.RasterCount\n",
    "#\n",
    "    geo = ds.GetGeoTransform()\n",
    "    srs = ds.GetProjection()\n",
    "#    dtype = ds.GetRasterBand(1).DataType\n",
    "    im = ds.ReadAsArray(0,0,nx,ny)\n",
    "#   \n",
    "    blue = im[0]\n",
    "    green = im[1]\n",
    "    red = im[2]\n",
    "    nir = im[3]\n",
    "    \n",
    "    ndvi = cal_ndvi(red,nir)\n",
    "    ndvi_dir = './输出数据'\n",
    "    out_ndvi_path = os.path.join(ndvi_dir ,'ndvi.tif')\n",
    "    saveim(out_ndvi_path,ndvi,srs=srs,geo=geo)\n",
    "#    minv,maxv = getminmaxatpercent(im[3,:,:])\n",
    "#    stretchim = stretch(im[3,:,:],minv,maxv,ngrey,1.8)\n",
    "#    padded = paddedbuf(stretchim,winsize)\n",
    "    \n",
    "#    data = im\n",
    "    basename = os.path.basename(imgfile).split('.')\n",
    "#    basedir = os.path.dirname(imgfile)\n",
    "    basedir = './输出数据'\n",
    "    for i in range(nb):\n",
    "        t1 = time.time()\n",
    "        minv,maxv = getminmaxatpercent(im[i,:,:])\n",
    "        stretchim = stretch(im[i,:,:],minv,maxv,ngrey,1.8)\n",
    "        padded = paddedbuf(stretchim,winsize)\n",
    "        mea,var,entr,con,diss,homo,corr,ASM = glcm_by_win_scan(padded,winsize,step = 1, angle = np.pi/4,level = ngrey)\n",
    "        \n",
    "        \n",
    "#        basename = ['input','tif']\n",
    "#        basedir = './'\n",
    "        outname = os.path.join(basedir,basename[0]+'_b{}_win{}_texture.tif'.format(i,winsize))\n",
    "    \n",
    "        saveim(outname,np.array([mea,var,entr,con,diss,homo,corr,ASM]),srs=srs,geo=geo)\n",
    "#        saveim(outname,np.array([mea,var,entr,con,diss,homo,corr,ASM]),srs=None,geo=None)\n",
    "        t2 = time.time()\n",
    "        \n",
    "        print('single process is ok! total time is {},processing file {},band {} \\n'.format((t2 -t1)/60.0,basename[0],i))\n",
    "        \n",
    "\n",
    "# def test_multiprocess():\n",
    "#     imgfile = './spot6.tif'\n",
    "    \n",
    "# #    nprocess = 4\n",
    "# #    paras = []\n",
    "    \n",
    "# #    imds = gdal.Open(imgfile)\n",
    "# #    if imds == None:\n",
    "# #        print('cannot open file {} \\n'.format(imgfile))\n",
    "# #        \n",
    "# #    imX = imds.RasterXSize\n",
    "# #    imY = imds.RasterYSize\n",
    "    \n",
    "# #    并行运算，分块的y的大小\n",
    "# #    nblock = int(imY/ nprocess)\n",
    "#     winsize = 3\n",
    "\n",
    "#     ds = gdal.Open(imgfile)\n",
    "#     if ds == None:\n",
    "#         print('cannot open file {} \\n'.format(imgfile))\n",
    "#     nx = ds.RasterXSize\n",
    "#     ny = ds.RasterYSize\n",
    "    \n",
    "#     nb = ds.RasterCount\n",
    "\n",
    "#     geo = ds.GetGeoTransform()\n",
    "#     srs = ds.GetProjection()\n",
    "#     dtype = ds.GetRasterBand(1).DataType\n",
    "#     im = ds.ReadAsArray(0,0,nx,ny)\n",
    "#     t1 = time.time()\n",
    "# #    for i in range(nb):\n",
    "        \n",
    "#     minv,maxv = getminmaxatpercent(im[3,:,:])\n",
    "#     stretchim = stretch(im[3,:,:],minv,maxv,1.8)\n",
    "#     padded = paddedbuf(stretchim,winsize)\n",
    "   \n",
    "#     ny2 = 500\n",
    "#     padded2 = padded[:ny2,0:100]\n",
    "\n",
    "#     ny22 = ny2 - winsize\n",
    "#     cors = 100\n",
    "#     pool = mp.Pool()    \n",
    "# #    pool.starmap(glcm_by_win_scan(imgbuf,winsize),zip_args)\n",
    "#     #data_split = np.array_split(padded2,cors,axis = 0)\n",
    "#     data_split = split_data(padded2,cors,winsize)\n",
    "#     print(len(data_split))\n",
    "#     out = pool.map(glcm_by_win_scan,data_split)\n",
    "#     #out = pool.starmap(glcm_by_win_scan,[(padded2[i:i+winsize,:],winsize) for i in range(ny22)])\n",
    "#     pool.close()\n",
    "#     #pool.join()\n",
    "# #    basename = os.path.basename(imgfile).split('.')\n",
    "# #    basedir = os.path.dirname(imgfile)\n",
    "# #    outname = os.path.join(basedir,basename[0]+'_b{}_texture.tif'.format(3))\n",
    "# #    saveim_multiproc(outname,np.array(out,dtype= np.float),srs,geo)\n",
    "\n",
    "#     t2 = time.time()\n",
    "#     print('multi process is ok! total time is {}'.format((t2 -t1)/60.0))\n",
    "\n",
    "# def split_data(data,ncors,winsize):\n",
    "#     nr,nc = data.shape\n",
    "#     blocksize = int(nr/ncors)\n",
    "\n",
    "#     bl_s = []\n",
    "#     bl_e = []\n",
    "#     for i in range(0,nr,blocksize):\n",
    "#         if i ==0:\n",
    "#             bl_s.append(i)\n",
    "#         else:\n",
    "#             bl_s.append(i - int(winsize/2))\n",
    "#         if i+blocksize+int(winsize/2)>nr:\n",
    "#             bl_e.append(nr)\n",
    "#         else:\n",
    "#             bl_e.append(i+blocksize+int(winsize/2))\n",
    "#     split_data = []\n",
    "#     for i,e in zip(bl_s,bl_e):\n",
    "#         split_data.append(data[i:e,:])     \n",
    "#     return split_data"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "single process is ok! total time is 25.690423452854155,processing file spot6卫星影像,band 0 \n",
      "\n",
      "single process is ok! total time is 18.276579558849335,processing file spot6卫星影像,band 1 \n",
      "\n",
      "single process is ok! total time is 17.879997483889262,processing file spot6卫星影像,band 2 \n",
      "\n",
      "single process is ok! total time is 16.225283753871917,processing file spot6卫星影像,band 3 \n",
      "\n"
     ]
    }
   ],
   "source": [
    "if __name__== '__main__':\n",
    "    imgfile = './使用数据/spot6卫星影像.tif'\n",
    "    winsize = 7\n",
    "    single_proc_file(imgfile,winsize,ngrey = 256)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
